package www.dianbaozu.com.ui.my

import androidx.databinding.ObservableField
import androidx.hilt.lifecycle.ViewModelInject
import androidx.lifecycle.viewModelScope
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.launch
import okhttp3.MultipartBody
import www.dianbaozu.com.R
import www.wen.com.common.lib.base.module.BaseApiViewModel
import www.wen.com.common.lib.data.entities.*
import www.wen.com.common.lib.data.repository.AppDataManager
import www.wen.com.common.lib.extension.getString
import www.wen.com.common.lib.util.ReadJsonFileUtil


/**
 * @author WEN
 * @Description:个人信息的页面
 * @date 2020//25 17:48
 */
class PersonalDetailsViewModel @ViewModelInject constructor(appDataManager: AppDataManager) :
        BaseApiViewModel(appDataManager) {

    //省市区的数据
    var options1Items: List<CityEntity.Content> = mutableListOf()
    val options2Items: MutableList<MutableList<CityEntity.Content>> = mutableListOf()
    val options3Items: MutableList<MutableList<MutableList<CityEntity.Content>>> = mutableListOf()

    var mOriginalHasImg = false

    var mOriginalNickname = ""

    var mOriginalSignature = ""

    var mOriginalGender = 0

    var mOriginalBirthday = ""

    var mOriginalArea = ""

    var mOriginalMail = ""

    var mHasImg = false
        set(value) {
            field = value
            checkIsCanSave()
        }

    var mNickname = ""
        set(value) {
            field = value
            checkIsCanSave()
        }

    var mSignature = ""
        set(value) {
            field = value
            checkIsCanSave()
        }

    var mGender = 0
        set(value) {
            field = value
            checkIsCanSave()
        }

    var mBirthday = ""
        set(value) {
            field = value
            checkIsCanSave()
        }

    var mArea = ""
        set(value) {
            field = value
            checkIsCanSave()
        }

    var mMail = ""
        set(value) {
            field = value
            checkIsCanSave()
        }

    val mEnableSave = ObservableField(false)

    fun checkIsCanSave() {
        if (mHasImg != mOriginalHasImg || mNickname != mOriginalNickname || mSignature != mOriginalSignature || mGender != mOriginalGender || mBirthday != mOriginalBirthday || mArea != mOriginalArea || mMail != mOriginalMail) {
            mEnableSave.set(true)
        } else {
            mEnableSave.set(false)
        }
    }

    fun requestArea() {
        val type = object : TypeToken<List<CityEntity.Content>>() {}.type
        val province: List<CityEntity.Content> = Gson().fromJson(ReadJsonFileUtil.readJsonFile("city.json"), type)
        options1Items = province
        province.forEach {
            //遍历省份
            val city = mutableListOf<CityEntity.Content>()
            val district = mutableListOf<MutableList<CityEntity.Content>>()
            it.childArea.forEach { content ->
                city.add(content)
                val cityAerialist = mutableListOf<CityEntity.Content>()
                cityAerialist.addAll(content.childArea)
                district.add(cityAerialist.toMutableList())
            }
            options2Items.add(city)
            options3Items.add(district)
        }
    }


    /**
     * 更新用户信息
     */
    fun requestUpdateUserInfo(
            partMap: Map<String, okhttp3.RequestBody>,
            body: RequestBodyParameter,
            vararg files: MultipartBody.Part,
            action: ((UseInfoEntity) -> Unit)? = null,
    ) {
        viewModelScope.launch {
            if(mHasImg){
                appDataManager.uploadOssFileFlow(partMap, *files)
                        .flatMapConcat {
                            appDataManager.userInfoFlow(body.apply {
                                avatar = it.data.content[0]
                            })
                        }
                        .onStart {
                            loading.value = UiStateCustom(
                                    stateData = LoadingDialog(
                                            getString(R.string.be_updating),
                                            LoadingDialogStateType.LOADING
                                    )
                            )
                        }
                        .onCompletion { cause ->
                            if (cause == null) {
                                loading.value = UiStateCustom(
                                        stateData = LoadingDialog(
                                                tip = getString(R.string.created_successfully),
                                                loadingDialogStateType = LoadingDialogStateType.SUCCESS,
                                                isShowDialogTip = false,
                                                isShowToastTip = false
                                        )
                                )
                            }
                        }
                        .catch { cause ->
                            loading.value = UiStateCustom(
                                    stateData = LoadingDialog(
                                            tip = cause.message ?: "网络异常",
                                            loadingDialogStateType = LoadingDialogStateType.ERROR,
                                            isShowDialogTip = false,
                                            isShowToastTip = false
                                    )
                            )
                        }
                        .collect {
                            action?.invoke(it.data)
                        }
            }else{
                appDataManager.userInfoFlow(body)
                        .onStart {
                            loading.value = UiStateCustom(
                                    stateData = LoadingDialog(
                                            getString(R.string.be_updating),
                                            LoadingDialogStateType.LOADING
                                    )
                            )
                        }
                        .onCompletion { cause ->
                            if (cause == null) {
                                loading.value = UiStateCustom(
                                        stateData = LoadingDialog(
                                                tip = getString(R.string.created_successfully),
                                                loadingDialogStateType = LoadingDialogStateType.SUCCESS,
                                                isShowDialogTip = false,
                                                isShowToastTip = false
                                        )
                                )
                            }
                        }
                        .catch { cause ->
                            loading.value = UiStateCustom(
                                    stateData = LoadingDialog(
                                            tip = cause.message ?: "网络异常",
                                            loadingDialogStateType = LoadingDialogStateType.ERROR,
                                            isShowDialogTip = false,
                                            isShowToastTip = true
                                    )
                            )
                        }
                        .collect {
                            action?.invoke(it.data)
                        }
            }
        }
    }


}


